package se.ramfelt.psnfriends.model;

/**
 * Friend in the PSN network
 */
public class Friend {

    public enum Presence {
        Online, Away, Offline
    }

    private String onlineId;
    private String avatarUrl;
    private String comment;
    private Trophies trophies;
    private Presence presence;
    private String currentGame;

    public Friend() {
        super();
    }

    public Friend(String onlineId) {
        this(onlineId, Presence.Online, null);
    }

    public Friend(String onlineId, Presence presence) {
        this(onlineId, presence, null);
    }

    public Friend(String onlineId, Presence presence, String currentGame) {
        setOnlineId(onlineId);
        setPresence(presence);
        setCurrentGame(currentGame);
    }

    public String getOnlineId() {
        return onlineId;
    }

    public void setOnlineId(String onlineId) {
        this.onlineId = onlineId;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = normalizeString(comment, "");
    }

    public Presence getPresence() {
        return presence;
    }

    public void setPresence(Presence presence) {
        this.presence = presence;
    }

    public void setPresence(String presenceStr) {
        if (presenceStr.toLowerCase().startsWith("online")) {
            this.presence = Presence.Online;
        } else if (presenceStr.toLowerCase().startsWith("offline")) {
            this.presence = Presence.Offline;
        } else if (presenceStr.toLowerCase().startsWith("away")) {
            this.presence = Presence.Away;
        } else {
            this.presence = Presence.Offline;
        }
    }

    public Trophies getTrophies() {
        return trophies;
    }

    public void setTrophies(Trophies trophies) {
        this.trophies = trophies;
    }

    public String getCurrentGame() {
        return currentGame;
    }

    public void setCurrentGame(String currentGame) {
        this.currentGame = normalizeString(currentGame, null);
    }

    @Override
    public String toString() {
        return "Friend [onlineId=" + onlineId + ", avatarUrl=" + avatarUrl + ", comment=" + comment + ", trophies=" + trophies + ", presence=" + presence + "]";
    }

    private String normalizeString(String str, String defaultStr) {
        if ((str == null) || (str.equals("null"))) {
            return defaultStr;
        } else {
            return str;
        }
    }
}
